﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Esign.Net
{
    public class DigestHelper
    {
        /// <summary>
        ///  HmacSHA256 加密
        /// </summary>
        /// <param name="secret">projectSecret</param>
        /// <param name="data">请求的JSON参数</param>
        /// <returns></returns>
        public static string GetSignature(string data, string secret)
        {
            byte[] keyByte = Encoding.UTF8.GetBytes(secret);
            byte[] messageBytes = Encoding.UTF8.GetBytes(data);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                StringBuilder sb = new StringBuilder();
                foreach (byte test in hashmessage)
                {
                    sb.Append(test.ToString("x2"));
                }
                return sb.ToString();
            }
        }

        #region 计算请求签名值
        /// <summary>
        /// HmacSHA256 计算请求签名值的Base64
        /// </summary>
        /// <param name="message"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static string doSignatureBase64(String message, String secret)
        {
            byte[] keyByte = Encoding.UTF8.GetBytes(secret);
            byte[] messageBytes = Encoding.UTF8.GetBytes(message);
            using (var hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
                return Convert.ToBase64String(hashmessage);
            }
        }
        #endregion

        #region 获取MD5
        /// <summary>
        /// 获取MD5
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string doContentMD5(String str)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            return Convert.ToBase64String(data).ToString();
        }

        #endregion



        /// <summary>
        /// 计算文件内容的MD5,其MD5值是一个128位（128bit）的二进制数组，对该数字进行base64编码为一个的Content-MD5值
        /// </summary>
        /// <returns></returns>
        public static string GetMD5FromStr(string message, bool MD5_Mode)
        {
            byte[] result = Encoding.UTF8.GetBytes(message);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            if (MD5_Mode == true)
            {
                return BitConverter.ToString(output).Replace("-", "");//32位MD5值
            }
            else
            {
                return BitConverter.ToString(output, 4, 8).Replace("-", "");//16位MD5值
            }

        }

        /// <summary>
        /// 时间戳计时开始时间
        /// </summary>
        private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        /// <summary>    
        /// 获取当前时间戳（毫秒级）    
        /// </summary>        
        /// <returns>long</returns>    
        public static long GetTimestamp()
        {
            return GetTimestamp(DateTime.Now);
        }

        /// <summary>    
        /// 获取当前时间戳（毫秒级）    
        /// </summary>        
        /// <returns>long</returns>    
        public static long GetTimestamp(DateTime dateTime)
        {
            return (long)(dateTime.ToUniversalTime() - Jan1st1970).TotalMilliseconds;
        }

    }
}
